package com.jshop.admin.sys.controller;

import cn.hutool.crypto.digest.DigestUtil;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.jshop.admin.sys.entity.SysUserEntity;
import com.jshop.admin.sys.entity.params.LoginParams;
import com.jshop.admin.sys.service.SysRoleMenuService;
import com.jshop.admin.sys.service.SysUserService;
import com.jshop.common.result.CommonResult;
import com.jshop.common.utils.ShiroUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 登录相关
 */
@Controller
public class SysLoginController extends AbstractController {
    @Autowired
    private Producer producer;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @ApiIgnore
    @RequestMapping("login")
    public String login() {
        return "login";
    }

    @RequestMapping("index")
    public String index() {
        return "index";
    }

    @ApiIgnore
    @RequestMapping("unauthorized")
    public CommonResult unauthorized() {
        System.out.println("认证不通过");
        return CommonResult.forbidden(null);
    }


    @ApiOperation("验证码")
    @GetMapping("captcha.jpg")
    public void captcha(HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //生成文字验证码
        String text = producer.createText();
        //生成图片验证码
        BufferedImage image = producer.createImage(text);
        //保存到shiro session
        ShiroUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
    }

    /**
     * 登录
     */
    @PostMapping(value = "/sys/login")
    @ResponseBody
    public CommonResult login(@RequestBody LoginParams loginParams) {

//        String kaptcha = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
//        if(null == kaptcha){
//            return CommonResult.failed("验证码已失效");
//        }
//        if (!captcha.equalsIgnoreCase(kaptcha)) {
//            return CommonResult.failed("验证码不正确");
//        }
        SysUserEntity sysUserEntity = sysUserService.queryByUserName(loginParams.getUserName());
        if (sysUserEntity == null) {
            return CommonResult.failed("账号或密码不正确");
        }
        if (!sysUserEntity.getPassword().equals(DigestUtil.md5Hex(loginParams.getPassword()))) {
            return CommonResult.failed("密码不正确");
        }
        if (sysUserEntity.getStatus() == 0) {
            return CommonResult.failed("账号已经被冻结，请联系管理员");
        }
        Subject subject = ShiroUtils.getSubject();
        //sha256加密
        String password = DigestUtil.md5Hex(loginParams.getPassword());
        UsernamePasswordToken token = new UsernamePasswordToken(loginParams.getUserName(), password);
        subject.login(token);
        return CommonResult.successMsg("");
    }

    /**
     * 退出
     */
    @ApiOperation("退出")
    @GetMapping(value = "/sys/logout")
    public CommonResult logout() {
        ShiroUtils.logout();
        return CommonResult.successMsg("成功");
    }


}
